package com.flowerplus.store.common.structure.ui.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flowerplus.store.common.data.injector.HasComponent;
import com.flowerplus.store.common.data.utils.DensityUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by z on.
 * Des:dialog fragment 基类
 * Date: 2017/3/30
 */

public abstract class BaseFragmentDialog extends DialogFragment {

    protected View mRootView;
    private Unbinder mBind;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE,0);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(layoutViewId(), container, false);
        mBind = ButterKnife.bind(this, mRootView);
        initViews();
        initDatas();
        initEvents();

        return mRootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mBind != null) {
            mBind.unbind();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        int width = (int) (DensityUtil.getScreenSize().x * 0.9);
        getDialog().getWindow().setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public abstract int layoutViewId();

    public abstract void initViews();

    protected abstract void initDatas();

    protected abstract void initEvents();


    protected <T extends View> T findViewById(int id) {
        return (T) mRootView.findViewById(id);
    }

    protected <C> C getComponent(Class<C> componentType) {
        return componentType.cast(((HasComponent<C>) getActivity()).getComponent());
    }


}
